package com.snake.springboot.custom.service.impl.goods;

import com.snake.springboot.custom.api.model.goods.StrategyGoodsModel;
import com.snake.springboot.custom.api.model.goods.StryGoodsDetailModel;
import com.snake.springboot.custom.api.response.goods.StrategyGoodsRsp;
import com.snake.springboot.custom.api.service.goods.StryGoodsDetailService;
import com.snake.springboot.custom.dao.mybatis.entity.goods.StryGoodsDetailDO;
import com.snake.springboot.custom.dao.mybatis.mappers.goods.StryGoodsDetailMapper;
import org.snake.common.utils.JsonUtils;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StryGoodsDetailServiceImpl implements StryGoodsDetailService{

    @Autowired
    private StryGoodsDetailMapper stryGoodsDetailMapper;

    @Override
    public void addDetail(StrategyGoodsModel model) {
        this.delByParentId(model.getStrategyGoodsId());
        List<StryGoodsDetailDO> dos = ListUtils.copy(model.getDetailList(), StryGoodsDetailDO.class);

        System.out.println(JsonUtils.toJson(dos));
        stryGoodsDetailMapper.bathAdd(dos);
    }

    @Override
    public void delByParentId(String id) {
        stryGoodsDetailMapper.deleteByStrategyId(id);
    }

    @Override
    public void queryDetail(List<StrategyGoodsRsp> rsps) {
        Set<String> ids = rsps.stream().map(StrategyGoodsRsp::getStrategyGoodsId).collect(Collectors.toSet());
        List<StryGoodsDetailDO> dos = stryGoodsDetailMapper.queryByStrategyIds(ids);
        List<StryGoodsDetailModel> models = ListUtils.copy(dos, StryGoodsDetailModel.class);
        for (StrategyGoodsRsp rsp : rsps) {
            rsp.setDetails(models.stream().filter(model -> model.getStrategyGoodsId().equals(rsp.getStrategyGoodsId())).collect(Collectors.toList()));
        }
    }
}
